﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace BookSystem
{
    class DBHepler
    {
        public static string connstring = "Data Source=.;Initial Catalog=bookSystem;Integrated Security=True";
        public static SqlConnection conn;

        //连接
        public static void SqlConnection()
        {
            conn = new SqlConnection(connstring);
            conn.Open();
            if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
        }

        //增删改
        public static int SqlCommand(string sql)
        {
            SqlConnection();
            SqlCommand zxc = new SqlCommand(sql, conn);
            int num = zxc.ExecuteNonQuery();
            conn.Close();
            return num;
        }

        //查
        public static DataTable SqlDataAdapter(string sql)
        {
            SqlConnection();
            SqlDataAdapter zxda = new SqlDataAdapter(sql, conn);
            DataTable table = new DataTable();
            zxda.Fill(table);
            conn.Close();
            return table;
        }
    }
}
